package de.tapps.brainstorming;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.AsyncTask;
import android.widget.TextView;
/**
 * Return the topic title and the content to a specific topic (an id)
 * 
 * @author Timo
 * Decrypted - ok
 */
public class GetTopic extends AsyncTask<String, Integer, String> {
	private int id;
	private String topicdescr;
	private String topic;
	private Crypter crypter;
	private MainActivity activity = new MainActivity();
	public GetTopic(MainActivity act,int pid) {
		id = pid;
		activity = act;
		crypter = new Crypter(activity);

	}

	@Override
	protected String doInBackground(String... arg0) {
		new Thread(new Runnable() {
			public void run() {
				URL url = null;
				try {
					url = new URL(
							"http://t-apps-online.de/BrainStorming/getTopic.php?id="
									+ id);
					String readLine = null;
					int counter = 0;
					BufferedReader buffReader = new BufferedReader(
							new InputStreamReader(url.openStream()));
					while ((readLine = buffReader.readLine()) != null) {
						if (counter % 2 == 0) {
						topic = crypter.decrypt(readLine);
						} else {
						topicdescr = crypter.decrypt(readLine);
						}
						counter++;
					}
				} catch (MalformedURLException me) {
					me.printStackTrace();
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
				activity.runOnUiThread(new Runnable() {
					public void run() {
						((TextView) activity.findViewById(R.id.answer_topic))
								.setText(topic);
					}
				});
				activity.runOnUiThread(new Runnable() {
					public void run() {
						((TextView) activity.findViewById(R.id.answer_topicdescr))
								.setText(topicdescr);
					}
				});
			}
		}).start();
		return null;
	}
}